package com.beaven.imserver.net.service;

import com.beaven.imserver.net.bean.api.account.AccountRspModel;
import com.beaven.imserver.net.bean.api.account.LoginModel;
import com.beaven.imserver.net.bean.api.account.RegisterModel;
import com.beaven.imserver.net.bean.api.base.ResponseModel;
import com.beaven.imserver.net.bean.db.User;
import com.beaven.imserver.net.factory.UserFactory;
import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

/**
 * author: Beaven
 * time: 2017/5/17 9:16
 *
 * 提供注册和登录操作
 */
@Path("/account")
public class AccountService extends BaseService {

  // 注册
  @POST
  @Path("/register")
  // 指定请求与返回的相应体为JSON
  @Consumes(MediaType.APPLICATION_JSON)
  @Produces(MediaType.APPLICATION_JSON)
  public ResponseModel<AccountRspModel> register(RegisterModel model) {
    if (!RegisterModel.check(model)) {
      // 参数错误
      return ResponseModel.buildParameterError();
    }

    User user = UserFactory.findByAccount(model.getAccount().trim());
    if (user != null) {
      // 已有账户
      return ResponseModel.buildHaveAccountError();
    }
    user = UserFactory.findByName(model.getName().trim());
    if (user != null) {
      // 已有用户名
      return ResponseModel.buildHaveNameError();
    }
    // 注册操作
    user = UserFactory.register(model.getAccount(), model.getPassword(), model.getName());
    if (user != null) {
      // 帐号存在，登录绑定token
      user = UserFactory.login(user);
      if (user != null) {
        // 绑定pushId
        return bind(user, model.getPushId());
      } else {
        // 登录失败
        return ResponseModel.buildLoginError();
      }
    } else {
      // 注册异常
      return ResponseModel.buildRegisterError();
    }
  }

  // 登录
  @POST
  @Path("/login")
  @Consumes(MediaType.APPLICATION_JSON)
  @Produces(MediaType.APPLICATION_JSON)
  public ResponseModel<AccountRspModel> login(LoginModel model) {
    if (!LoginModel.check(model)) {
      return ResponseModel.buildParameterError();
    }
    User user = UserFactory.login(model.getAccount(), model.getPassword());
    if (user != null) {
      // 帐号存在，登录绑定token
      user = UserFactory.login(user);
      if (user != null) {
        // 绑定pushId
        return bind(user, model.getPushId());
      } else {
        // 登录失败
        return ResponseModel.buildLoginError();
      }
    } else {
      //  帐号异常
      return ResponseModel.buildAccountError(null);
    }
  }

  @PUT
  @Path("/logout")
  @Consumes(MediaType.APPLICATION_JSON)
  @Produces(MediaType.APPLICATION_JSON)
  public ResponseModel<AccountRspModel> logout() {
    User user = getSelf();
    user.setToken(null);
    user.setPushId(null);
    user = UserFactory.updateUser(user);
    if (user != null) {
      AccountRspModel rspModel = new AccountRspModel(user);
      return ResponseModel.buildOk(rspModel);
    } else {
      return ResponseModel.buildServiceError();
    }
  }

  /**
   * 绑定的操作
   *
   * @param self 自己
   * @param pushId PushId
   * @return User
   */
  private ResponseModel<AccountRspModel> bind(User self, String pushId) {
    // 进行设备Id绑定的操作
    User user = UserFactory.bindPushId(self, pushId);

    if (user == null) {
      // 绑定失败则是服务器异常
      return ResponseModel.buildServiceError();
    }
    // 返回当前的账户, 并且已经绑定了
    AccountRspModel rspModel = new AccountRspModel(user, true);
    return ResponseModel.buildOk(rspModel);
  }
}
